/**
 * 
 */
package com.auxgroup.hr.front.dozer.vo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * Map类型的Value Object。 扩展了Map接口，并提供类型转换功能。
 * 
 */
public class QueryBean extends HashMap<String, Object> {

	private static final String DEFAULT_SPLIT = ",";
	/**
	 * 
	 */
	private static final long serialVersionUID = -8061689685611158256L;

	@SuppressWarnings("unchecked")
	public <T> T getObject(String key) {
		return (T) getObject(key, null);
	}

	@SuppressWarnings("unchecked")
	public <T> T getObject(String key, T defaultValue) {
		try {
			if (containsKey(key)) {
				return (T) get(key);
			}
		} catch (ClassCastException e) {
			// do nothing,cast failure
		}
		return defaultValue;
	}

	public Integer getInteger(String key) {
		return getInteger(key, null);
	}

	public Integer getInteger(String key, Integer defaultValue) {
		try {
			if (containsKey(key)) {
				return Integer.valueOf(get(key).toString());
			}
		} catch (Exception e) {
			// do nothing
		}
		return defaultValue;
	}

	public Long getLong(String key) {
		return getLong(key, null);
	}

	public Long getLong(String key, Long defaultValue) {
		try {
			if (containsKey(key)) {
				return Long.valueOf(get(key).toString());
			}
		} catch (Exception e) {
			// do nothing
		}
		return defaultValue;
	}

	public Float getFloat(String key) {
		return getFloat(key, null);
	}

	public Float getFloat(String key, Float defaultValue) {
		try {
			if (containsKey(key)) {

				return Float.valueOf(get(key).toString());
			}
		} catch (Exception e) {
			// do nothing
		}
		return defaultValue;
	}

	public Double getDouble(String key) {
		return getDouble(key, null);
	}

	public Double getDouble(String key, Double defaultValue) {
		try {
			if (containsKey(key)) {
				return Double.valueOf((String) get(key));
			}
		} catch (Exception e) {
			// do nothing
		}
		return defaultValue;
	}

	public Boolean getBoolean(String key) {
		return getBoolean(key, false);
	}

	public Boolean getBoolean(String key, Boolean defaultValue) {
		try {
			if (containsKey(key)) {
				return Boolean.valueOf(get(key).toString());
			}
		} catch (Exception e) {
			// do nothing
		}
		return defaultValue;
	}

	boolean isNull(String key) {
		if (null == key) {
			return true;
		}
		return !this.containsKey(key) || this.getObject(key) == null;
	}

	/**
	 * @param key
	 * @return Default is "" if not exist.
	 */
	public String getString(String key) {
		return this.getString(key, "");
	}

	/**
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String getString(String key, String defaultValue) {
		try {
			if (containsKey(key)) {
				Object value = get(key);
				if (value instanceof String) {
					return (String) value;
				}
			}
		} catch (Exception e) {
			// do nothing
		}
		return defaultValue;
	}

	/**
	 * @param key
	 * @param split
	 *            default is ',' if split is null.
	 * @return
	 */
	public String[] getAsStringVector(String key, String split) {
		String value = getString(key, null);
		if (null == split || "".equals(split)) {
			split = DEFAULT_SPLIT;
		}
		if (null!=value) {
			return value.split(split);
		}
		return new String[0];
	}

	/**
	 * @param key
	 * @return
	 */
	public String[] getAsStringVector(String key) {
		return getAsStringVector(key, DEFAULT_SPLIT);
	}

	/**
	 * @param key
	 * @param split
	 * @return
	 */
	public List<String> getAsStringList(String key, String split) {
		String[] asStringList = getAsStringVector(key, split);
		if (null != asStringList && 0 < asStringList.length) {
			return Arrays.asList(asStringList);
		}
		return new ArrayList<String>();
	}

	/**
	 * @param key
	 * @return
	 */
	public List<String> getAsStringList(String key) {
		return getAsStringList(key, DEFAULT_SPLIT);
	}

}
